package org.darkforest.merchant.controller;

import org.darkforest.common.services.GoodsClassRedisService;
import org.darkforest.common.services.GoodsClassService;
import org.darkforest.common.services.VoucherTemplateService;
import org.darkforest.common.util.DateUtil;
import org.darkforest.controller.WebController;
import org.darkforest.dao.mapper.GoodsClassMapper;
import org.darkforest.model.GoodsClass;
import org.darkforest.model.VoucherTpl;
import org.darkforest.vo.AjaxReturnData;
import org.darkforest.vo.AjaxReturnImageVO;
import org.darkforest.vo.StoreSellerVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2015/12/30.
 */
@RequestMapping(value = "promotion")
@Controller
public class CouponController extends WebController<CouponController> {

    @Autowired
    private VoucherTemplateService voucherTemplateService;


    @Autowired
    private GoodsClassRedisService goodsClassRedisService;

    @Autowired
    private GoodsClassService goodsClassService;

    @Autowired
    private GoodsClassMapper goodsClassMapper;

    /**
     * 优惠券管理列表
     * @param p
     * @param vtState
     * @param voucherTTitle
     * @param startTime
     * @param endTime
     * @return
     */
    @RequestMapping(value = "couponList")
    public ModelAndView couponList(@RequestParam(required = false, defaultValue = "1") int p,
                                   @RequestParam(required = false, defaultValue = "0") String  vtState,
                                   @RequestParam(required = false, defaultValue = "") String  voucherTTitle,
                                   @RequestParam(required = false, defaultValue = "") String  startTime,
                                   @RequestParam(required = false, defaultValue = "") String  endTime
                                   ){
        ModelAndView res = new ModelAndView("promotion/coupon/coupon_list");
        VoucherTpl voucherTpl = new VoucherTpl();
        voucherTpl.setStart(p);
        voucherTpl.setVoucherTState("0".equals(vtState) ? null : Byte.parseByte(vtState));
        voucherTpl.setVoucherTTitle("".equals(voucherTTitle) ? null : voucherTTitle);
        voucherTpl.setVoucherTStartDate("".equals(startTime) ? null : DateUtil.toUnixTimeStampByDate(startTime));
        voucherTpl.setVoucherTEndDate("".equals(endTime) ? null : DateUtil.toUnixTimeStampByDate(endTime));
        voucherTpl= voucherTemplateService.selectVoucherTplListByPage(voucherTpl);
        voucherTpl.setStartDate(startTime);
        voucherTpl.setEndDate(endTime);
        res.addObject("pageBean",voucherTpl);
        return res;
    }


    @RequestMapping(value = "toAddCoupon")
    public ModelAndView toAddCoupon(HttpServletRequest request){
//        StoreSellerVo store = (StoreSellerVo)request.getSession().getAttribute("sellerInfo");

        ModelAndView res = new ModelAndView("promotion/coupon/add_coupon");

        List<GoodsClass> goodsClassListAll = new ArrayList<GoodsClass>();

            List<GoodsClass> level1Gc = goodsClassMapper.findGoodsClass(new GoodsClass(0, false));
            for (GoodsClass goodsClass1 : level1Gc) {
                goodsClassListAll.add(goodsClass1);
//                List<GoodsClass> level2Gc =goodsClassRedisService.getGoodsClassList(goodsClass1.getGcId());
                List<GoodsClass> level2Gc = goodsClassMapper.findGoodsClass(new GoodsClass(goodsClass1.getGcId()));
                goodsClassListAll.addAll(level2Gc);
                for (GoodsClass goodsClass2 : level2Gc) {
                    goodsClass2.setGcName("&nbsp;&nbsp;&nbsp;&nbsp;" + goodsClass2.getGcName());
                    List<GoodsClass> level3Gc = goodsClassMapper.findGoodsClass(new GoodsClass(goodsClass2.getGcId()));
                    goodsClassListAll.addAll(level3Gc);
                    for(GoodsClass goodsClass3 : level3Gc){
                        goodsClass3.setGcName("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + goodsClass3.getGcName());
                    }
                }
            }

        res.addObject("goodsClassList",goodsClassListAll);
        return res;

    }






    /**
     * 新增优惠券
     * @param tpl
     * @return
     */
    @RequestMapping(value = "doAddCoupon")
    public String doAddCoupon(VoucherTpl tpl,HttpServletRequest request){
        StoreSellerVo store = (StoreSellerVo)request.getSession().getAttribute("sellerInfo");
        tpl.setVoucherTStartDate(DateUtil.toUnixTimeStampByDate(tpl.getStartDate()));
        tpl.setVoucherTEndDate(DateUtil.toUnixTimeStampByDate(tpl.getEndDate()));
        tpl.setVoucherTStoreId(store.getStoreId());
        tpl.setVoucherTCreatorId(store.getMemberId());
        tpl.setVoucherTStorename(store.getStoreName());
        tpl.setVoucherTAddDate(DateUtil.unixTimeStampInt().intValue());
        voucherTemplateService.insertSelective(tpl);

        return "redirect: couponList";
    }


    /**
     * 根据主键删除优惠券
     * @param id
     * @return
     */
    @RequestMapping(value = "deleteCouponById")
    public @ResponseBody
    AjaxReturnData deleteCouponById(Integer id){
        AjaxReturnData ajaxReturnData = new AjaxReturnData();

        int i = voucherTemplateService.deleteByPrimaryKey(id);

        if(i==1){
            ajaxReturnData.setMessage("优惠券删除成功");
            ajaxReturnData.setResult(true);
        }else{
            ajaxReturnData.setMessage("优惠券删除失败");
            ajaxReturnData.setResult(false);
        }

        return ajaxReturnData;
    }


    @RequestMapping(value = "toModifyCoupon")
    public ModelAndView toModifyCoupon(Integer id,HttpServletRequest request){
        StoreSellerVo store = (StoreSellerVo)request.getSession().getAttribute("sellerInfo");

        ModelAndView res = new ModelAndView("promotion/coupon/modify_coupon");
        //查询店铺分类
        List<GoodsClass> goodsClassListAll = new ArrayList<GoodsClass>();

        List<GoodsClass> level1Gc = goodsClassMapper.findGoodsClass(new GoodsClass(0, false));
        for (GoodsClass goodsClass1 : level1Gc) {
            goodsClassListAll.add(goodsClass1);
//                List<GoodsClass> level2Gc =goodsClassRedisService.getGoodsClassList(goodsClass1.getGcId());
            List<GoodsClass> level2Gc = goodsClassMapper.findGoodsClass(new GoodsClass(goodsClass1.getGcId()));
            goodsClassListAll.addAll(level2Gc);
            for (GoodsClass goodsClass2 : level2Gc) {
                goodsClass2.setGcName("&nbsp;&nbsp;&nbsp;&nbsp;" + goodsClass2.getGcName());
                List<GoodsClass> level3Gc = goodsClassMapper.findGoodsClass(new GoodsClass(goodsClass2.getGcId()));
                goodsClassListAll.addAll(level3Gc);
                for(GoodsClass goodsClass3 : level3Gc){
                    goodsClass3.setGcName("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + goodsClass3.getGcName());
                }
            }
        }

        res.addObject("goodsClassList",goodsClassListAll);
        res.addObject("coupon",voucherTemplateService.selectByPrimaryKey(id));

        return res;
    }



    @RequestMapping(value = "doModifyCoupon")
    public String doModifyCoupon(HttpServletRequest request,VoucherTpl voucherTpl){
        voucherTpl.setVoucherTStartDate(DateUtil.toUnixTimeStampByDate(voucherTpl.getStartDate()));
        voucherTpl.setVoucherTEndDate(DateUtil.toUnixTimeStampByDate(voucherTpl.getEndDate()));
        voucherTemplateService.updateByPrimaryKeySelective(voucherTpl);

        return "redirect: couponList";
    }

    /**
     * 上传优惠券图片
     * @param multipartHttpServletRequest
     * @return
     */
    @RequestMapping("uploadImage")
    public @ResponseBody
    AjaxReturnImageVO uploadStoreImage(MultipartHttpServletRequest multipartHttpServletRequest){
        AjaxReturnImageVO ajaxReturnData = new AjaxReturnImageVO();
        try {
            MultipartFile activeImageFile = multipartHttpServletRequest.getFile("upload_file");

            String  activeImage = super.uploadPicsWithoutCompressSize(activeImageFile, multipartHttpServletRequest, null, null);

            ajaxReturnData.setAjaxMsg("上传成功");
            ajaxReturnData.setReturnCode("true");
            ajaxReturnData.setImageName(activeImage);
        }catch (Exception e){
            e.printStackTrace();
        }
        return ajaxReturnData;
    }

}
